package Algorithm.StackAndQueue;

import java.util.Stack;

public class SortStackByStack {

    public static void sort(Stack<Integer> stack) {
        Stack<Integer> help = new Stack<>();
        help.push(Integer.MAX_VALUE);
        while (!stack.isEmpty()) {
            int cur = stack.pop();
            if (cur > help.peek()) {
                while (help.size() > 1) {
                    stack.push(help.pop());
                }
            }
            help.push(cur);
        }
        // 放回stack
        while (help.size() > 1) {
            stack.push(help.pop());
        }
    }

    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(3);stack.push(5);stack.push(1);stack.push(2);stack.push(7);stack.push(1);
        sort(stack);
        int top = stack.pop();
    }
}
